var w = window;
var ol = "onload";

w[ol] = function(){
	var tsObj = new TouchScroll(document.getElementById("pCon"));
	var tsObj2 = new TouchScroll(document.getElementById("imgCon"));
	//var tsObj2 = new TouchTransform(document.getElementById("imgCon"));
	/*
	var pItems = document.getElementById("pList").getElementsByTagName("a");
		for(var i = 0; i < pItems.length; i++){
			pItems[i].onclick = function(){
			document.getElementById("photoView").setAttribute("src",this.getAttribute("href"));
			return false;
		}
	}
	*/
}
function TouchScroll(con)
{
	this.con = con;
	
	this.con.addEventListener("touchstart", this, false);
}


TouchScroll.prototype.handleEvent = function(event)
{
	switch(event.type)
	{
		case "touchstart":
			this.onStart(event);
			break;
		case "touchmove":
			this.onMove(event);
			break;
		case "touchend":
			this.onEnd(event);
			break;
	}
}

TouchScroll.prototype.onStart = function(e)
{
	if (e.targetTouches.length != 1)
	{
		return false;
	}

	this.touchStartX = e.targetTouches[0].clientX;
	this.touchStartY = e.targetTouches[0].clientY;
	
	this.topStart = this.con.scrollTop;

    window.addEventListener("touchmove", this, false);
    window.addEventListener("touchend", this, false);
    
	document.getElementById("xAxis").value = this.touchStartX;
	document.getElementById("yAxis").value = this.touchStartY;
}

TouchScroll.prototype.onMove = function(e)
{
	e.preventDefault();

	if (e.targetTouches.length != 1)
	{
		return false;
	}
	this.setScroll(e.targetTouches[0]);
}

TouchScroll.prototype.onEnd = function(e)
{
	e.preventDefault();

	if (e.targetTouches.length > 0)
	{
		this.resetTouch(e.targetTouches[0]);
		return false;
	}
	window.removeEventListener("touchmove", this.onMove, false);
	window.removeEventListener("touchend", this.onEnd, false);
	return false;
}
TouchScroll.prototype.resetTouch = function(aTouch){
    this.touchStartX = aTouch.clientX;
    this.touchStartY = aTouch.clientY;
    this.setScroll(aTouch);
}

TouchScroll.prototype.setScroll = function(aTouch){
	this.touchMoveX = aTouch.clientX;
	this.touchMoveY = aTouch.clientY;

	this.con.scrollTop = this.topStart + (this.touchStartY - this.touchMoveY);

	document.getElementById("xAxisM").value = this.touchMoveX;
	document.getElementById("yAxisM").value = this.touchMoveY;
}

function TouchTransform(con)
{
	this.con = con;
	
	this.con.addEventListener("touchstart", this, false);
	this.imgCon = document.getElementById("photoView");
	
	this.startWidth = this.imgCon.width;
	this.startHeight = this.imgCon.height;
}
TouchTransform.prototype.handleEvent = function(event)
{
	switch(event.type)
	{
		case "touchstart":
			this.onStart(event);
			break;
		case "touchmove":
			this.onMove(event);
			break;
		case "touchend":
			this.onEnd(event);
			break;
	}
}
TouchTransform.prototype.onStart = function(e)
{
	if (e.targetTouches.length != 2)
	{
		return false;
	}

	this.touch1StartX = e.targetTouches[0].clientX;
	this.touch1StartY = e.targetTouches[0].clientY;
	this.touch2StartX = e.targetTouches[1].clientX;
	this.touch2StartY = e.targetTouches[1].clientY;
    
    var xLength = Math.abs(this.touch1StartX-this.touch2StartX);
    var yLength = Math.abs(this.touch1StartY-this.touch2StartY);
    
    this.startDistance = Math.sqrt((xLength*xLength) + (yLength*yLength));

	document.getElementById("dis1").value = this.startDistance;

    window.addEventListener("touchmove", this, false);
    window.addEventListener("touchend", this, false);
}

TouchTransform.prototype.onMove = function(e)
{
	e.preventDefault();

	if (e.targetTouches.length != 2)
	{
		return false;
	}

	this.touch1MoveX = e.targetTouches[0].clientX;
	this.touch1MoveY = e.targetTouches[0].clientY;
	this.touch2MoveX = e.targetTouches[1].clientX;
	this.touch2MoveY = e.targetTouches[1].clientY;
    
    var xLength = Math.abs(this.touch1MoveX - this.touch2MoveX);
    var yLength = Math.abs(this.touch1MoveY - this.touch2MoveY);

    this.newDistance = Math.sqrt((xLength*xLength) + (yLength*yLength));

	this.imgCon.style.height = (this.startHeight * (this.newDistance/this.startDistance)) + "px";
	this.imgCon.style.width = (this.startWidth * (this.newDistance/this.startDistance)) + "px";

	document.getElementById("dis2").value = this.newDistance;
    document.getElementById("imgHx").value = this.startHeight * (this.newDistance/this.startDistance);
    document.getElementById("imgWx").value = this.startWidth * (this.newDistance/this.startDistance);
    
	//this.imgCon.style.height = "100px";
	//this.imgCon.style.width = "100px";
}

TouchTransform.prototype.onEnd = function(e)
{
	e.preventDefault();

	window.removeEventListener("touchmove", this.onMove, false);
	window.removeEventListener("touchend", this.onEnd, false);
}







